﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    
    public partial class PageAlarmInfo : Form
    {
        public PageAlarmInfo()
        {
            InitializeComponent();
        }
        private List<AlarmInfo> lst_pros;
        private void btn_queryPro_Click(object sender, EventArgs e)
        {
            string querystr = "";
            string queryID = "";
            string queryResult = "";
            //      if (ckb_queryTime.Checked && !string.IsNullOrEmpty(dtp_q_timeStart.Text))
            {
                querystr += $"startTime >'{dtp_q_timeStart.Text}' and startTime <'{dtp_q_timeEnd.Text}'";
            }
            if (ckb_queryID.Checked && !string.IsNullOrEmpty(tbx_q_Id.Text))
            {
                if (string.IsNullOrEmpty(querystr))
                    querystr += $"ProIds like '%{tbx_q_Id.Text}%'";
                else
                    querystr += $"and ProIds like '%{tbx_q_Id.Text}%'";
            }

            if (!string.IsNullOrEmpty(querystr))
            {
                dgv_display.Rows.Clear();
                var lst = GlobalVar.sqlHelper.select<AlarmInfo>(GlobalVar.userConfig.db_T_Alarm, querystr);
                if (lst != null)
                {
                    int Index = 0;
                    lst.ForEach(info =>
                    {
                        Index++;
                        dgv_display.Rows.Add(info.startTime.ToString("yyyy-MM-dd HH:mm:ss"), info.AlarmMsg,info.ProIds);
                    });
                    lst_pros = lst;
                }

            }
        }

        private void btn_exportPro_Click(object sender, EventArgs e)
        {
            if (dgv_display.Rows.Count > 0 && lst_pros != null && lst_pros.Count > 0)
            {
                FolderBrowserDialog dirDialog = new FolderBrowserDialog();
                dirDialog.Description = "请选择导出文件夹";
                if (dirDialog.ShowDialog() == DialogResult.OK)
                {
                    var dirPath = dirDialog.SelectedPath;
                    WriteCSV(lst_pros, dirPath);
                }
            }
            else
            {
                MessageBox.Show("请先查询生产数据");
            }
        }
        public void WriteCSV(List<AlarmInfo> infoList, string dirPath)
        {
            string saveDate = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            var strPath = dirPath + "\\" + saveDate + ".CSV";
            FileStream fs = new FileStream(strPath, FileMode.CreateNew);
            StreamWriter csvStreamWriter = new StreamWriter(fs, Encoding.Default);
            string strSpile = ",";
            csvStreamWriter.WriteLine($"发生时间{strSpile}报警内容{strSpile}涉及产品ID");
            infoList.ForEach(info => {

                var str = info.startTime.ToString("yyyy-MM-dd HH:mm:ss") + strSpile + info.AlarmMsg + strSpile + info.ProIds;
                 
                csvStreamWriter.WriteLine(str);
            });
            csvStreamWriter.Flush();
            csvStreamWriter.Close();
            fs.Close();
        }

        private void PageAlarmInfo_Load(object sender, EventArgs e)
        {
            GlobalMotion.addAlarmInfo += addAlarmInfo;
        }
        private void addAlarmInfo(AlarmInfo info)
        {
            this.BeginInvoke(new Action(() =>
            {
                dgv_real.Rows.Add(new object[] { info.startTime.ToString("yyyy-MM-dd HH:mm:ss"), info.AlarmMsg, info.ProIds });
            }));
        }
    }
}
